﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Practices.ServiceLocation;
using System.Collections.ObjectModel;
using System.ComponentModel.Composition;

namespace Dynamic_UI_Framework
{
    public class Panel : IPanel
    {
        public ObservableCollection<IVisual> Items = new ObservableCollection<IVisual>();

        public FrameworkElement Target
        {
            get;
            set;
        }

        public string Name
        {
            get;
            set;
        }

        public IVisual Owner
        {
            get;
             set;
        }

        public void Activate(IVisual v)
        {
            if (Owner != null)
                Owner.Activate(this);

        }

        public List<MenuItem> GetMenuItem()
        {
            List<MenuItem> list = new List<MenuItem>();
            var mapping = ServiceLocator.Current.GetInstance<ElementAdapterMapping>();
            foreach (var item in mapping.Mappings)
            {
                MenuItem mi = new MenuItem();
                mi.Header = item.Key;
                mi.Tag = item.Value;
                mi.Click += (s, e) =>
                {
                    var adapter = mi.Tag as IAdapter;
                    string name = DialogHelper.DialogName(string.Empty);
                    if (string.IsNullOrEmpty(name))
                    {
                        MessageBox.Show("string.IsNullOrEmpty(name)");
                        return;
                    }
                    IElement element = ServiceLocator.Current.GetInstance<IElement>(mi.Header.ToString());
                    (element as FrameworkElement).Width = 100;
                    (element as FrameworkElement).Height = 100;
                    IVisual v = adapter.Initialize(element, name);

                    var mp = ServiceLocator.Current.GetInstance<DesignerItemAdapterMapping>();
                    IAdapter ad = mp.GetMapping(typeof(DesignerItemControl));
                    IVisual vv = ad.DefaultBuild("DesignerItemControl");
                    v.Owner = vv;
                    (vv as DesignerItem).CurrentItem = v;
                    vv.Owner = this;
                    this.Items.Add(vv);
                };
                list.Add(mi);
            }
            return list;
        }
    }


    [Export("Panel", typeof(IBuilder))]
    public class PanelBuilder : IBuilder
    {
        public UINode GetUINode(IVisual obj)
        {
            Panel panel = obj as Panel;
            if (panel == null)
                return null;

            UINode node = new UINode();
            node.Target = panel;
            node.Name = panel.Name;
            node.TypeName = panel.GetType().Name;
            node.Properties.Add("target", panel.Target.GetType().FullName);

            foreach (var item in panel.Items)
            {
                string key = item.GetType().Name;
                IBuilder builder = ServiceLocator.Current.GetInstance<IBuilder>(key);
                if (builder == null)
                    throw new Exception("builder == null");

                UINode child = builder.GetUINode(item);
                child.Owner = node;
                node.Children.Add(child);
            }
            return node;
        }
        public IVisual Build(UINode node)
        {
            string typeFullName = node.Properties.ContainsKey("target") ? node.Properties["target"].ToString() : null;
            if (string.IsNullOrEmpty(typeFullName))
                throw new Exception("string.IsNullOrEmpty(typeFullName)");

            PanelAdapterMapping mapping = ServiceLocator.Current.GetInstance<PanelAdapterMapping>();
            IAdapter adapter = mapping.GetMappingByTypeFullName(typeFullName);
            if (adapter == null)
                throw new Exception("adapter == null");

            Panel panel = adapter.DefaultBuild(node.Name) as Panel;
            foreach (var item in node.Children)
            {
                IBuilder builder = ServiceLocator.Current.GetInstance<IBuilder>(item.TypeName);
                IVisual child = builder.Build(item) as IVisual;
                child.Owner = panel;
                panel.Items.Add(child);
            }
            return panel;
        }
    }
}
